
#include "app_menu.h" 
#include "menu_msg.h" 
#include "isd.h" 
#include "macros.h" 
#include "App.h" 
#include "OS.h" 
#include "SysClock.h" 
#include "debug.h" 

//int am_msg_Welcome[] = 				{AMSG_WELCOME, 0x200, 0x210, 0x220, 0x80, AMSG_END_OF_MSG};
int am_msg_Welcome[]        =		{AMSG_WELCOME, AMSG_END_OF_MSG};
int am_msg_EnterPassw[] 	 =		{AMSG_ENTER_PASSWORD, AMSG_END_OF_MSG};
int am_msg_ChangeOutputs[]  =		{AMSG_FOR_ACTIVE_PRESS,AMSG_END_OF_MSG};
int am_msg_ChangeOutput_1[] = 	{AMSG_OUTPUT_1_NAME, AMSG_OUTPUT_STATE_OFF, AMSG_TO_CHANGE_PRESS, AMSG_1,AMSG_END_OF_MSG};
int am_msg_ChangeOutput_2[] = 	{AMSG_OUTPUT_2_NAME, AMSG_OUTPUT_STATE_OFF, AMSG_TO_CHANGE_PRESS, AMSG_1,AMSG_END_OF_MSG};
int am_msg_ChangeOutput_3[] = 	{AMSG_OUTPUT_3_NAME, AMSG_OUTPUT_STATE_OFF, AMSG_TO_CHANGE_PRESS, AMSG_1,AMSG_END_OF_MSG};
int am_msg_Config[]			 = 	
{AMSG_CHANGE_OUTPUT_TIMEOUT, AMSG_PRESS, AMSG_1, 
 AMSG_CHANGE_PASSWORD, 		  AMSG_PRESS, AMSG_2,
 AMSG_NAME_OUTPUT_RECORD,    AMSG_PRESS, AMSG_3,
 AMSG_END_OF_MSG};

int am_msg_WelcomePassw[] = {AMSG_WELCOME, AMSG_ENTER_PASSWORD, AMSG_END_OF_MSG};

int am_msg_Config_ChangeOutputTimeout[] = 
{AMSG_CHOOSE_TIME_UINT, AMSG_PRESS, AMSG_1, 
 AMSG_CHANGE_TIMEOUT,   AMSG_PRESS, AMSG_2, 
 AMSG_END_OF_MSG};

int am_msg_Config_ChangeOutputTimeout_Unit[] = 
{AMSG_TIME_UINT, AMSG_TIME_UNIT_SECOND, AMSG_TO_CHANGE_PRESS, AMSG_1,AMSG_END_OF_MSG};

int am_msg_Config_ChangeOutputTimeout_Timeout[] = {AMSG_CHANGE_TIMEOUT, AMSG_PLEASE_ENTER_NUMBER, AMSG_END_OF_MSG};

int am_msg_Config_ChangeOutputsName[] = 
{AMSG_TO_CHANGE, AMSG_NAME, 
 AMSG_OUTPUT_1_NAME, AMSG_PRESS, AMSG_1,
 AMSG_OUTPUT_2_NAME, AMSG_PRESS, AMSG_2,
 AMSG_OUTPUT_3_NAME, AMSG_PRESS, AMSG_3,
 AMSG_END_OF_MSG};

int am_msg_Config_ChangeOutputsName_Record[] = {AMSG_SPEAK_NEW_NAME, AMSG_AFTER_BEEP, AMSG_BEEP, AMSG_END_OF_MSG};

void am_audio_on ( int onoff )
{
	if ( onoff )
	{
		isd_cmd(0, ISD_CMD_POWERUP);
		delay_ms(25);
		isd_cmd(0, ISD_CMD_POWERUP);
		delay_ms(50);
	}
	else
	{
		isd_cmd(0, ISD_CMD_STOPPWRDN);
	}
	
} 

__inline__ void am_play_msg ( int msg )
{
	isd_cmd(msg, ISD_CMD_SETPLAY);	
} 

__inline__ void am_record_msg ( int msg )
{
	isd_cmd(msg, ISD_CMD_SETREC);	
} 

__inline__ void am_stop_msg ( void )
{
	isd_cmd(0,ISD_CMD_STOP);
}

void msg_play ( void )
{
	switch(APP.State)
	{
		case ASTE_START:
			OS_SET_TIMER(20000, ASTE_FINISH);
			DBG("\n%s; msg_play__msg = 0x%X\n",__FUNCTION__,msg_play__msg); 
			if ( msg_play__msg != AMSG_END_OF_MSG )
			{
				am_play_msg(msg_play__msg);
				APP.State = ASTE_WAIT_FOR_EOM;
			}
			else
			{
				DBG("\n%s; ERROR: msg_play__msg == AMSG_END_OF_MSG\n",__FUNCTION__); 
				APP.Result = AR_FAIL;
				APP.State = ASTE_FINISH;
			}
			
			break;

		case ASTE_WAIT_FOR_EOM:
			if ( isd_message_status != ISD_MSG_EOM )
			{
				return;
			}
			APP.Result = AR_SUCCSESS;

		case ASTE_FINISH:
			am_stop_msg();
			DBG("\n%s; FINISH\n",__FUNCTION__); 
			am_stop_msg();
			OS_Return();
			
	}
}

int *msg_play__msgs;
int msg_play__msg;

void msgs_play ( void )
{
	switch(APP.State)
	{
		case ASTE_START:
			DBG("\n%s;\n",__FUNCTION__);
			OS_SET_TIMER(20000, ASTE_FINISH);
			APP.State = ASTE_PLAY_MSG;
						
		case ASTE_PLAY_MSG:
			if ( APP.CallResult ) msg_play__msgs++;
			if ( *msg_play__msgs != AMSG_END_OF_MSG )
			{
				msg_play__msg = *msg_play__msgs;
				OS_Call_App(1,msg_play);
			}
			else
			{
				APP.State = ASTE_FINISH;
			}
			break;

		case ASTE_FINISH:
			msg_play__msgs = NULL;
			am_stop_msg();
			DBG("\n%s; FINISH\n",__FUNCTION__);
			OS_Return();
	}
}
